<?php

namespace Core\Forms\Init;

use Core\Forms\Form;
use Core\Forms\Element\Autoinput;
use Core\Forms\Element\Chosen;
use Core\Forms\Element\Date;
use Core\Forms\Element\Email;
use Core\Forms\Element\File;
use Core\Forms\Element\Check;
use Core\Forms\Element\Group;
use Core\Forms\Element\GroupTabs;
use Core\Forms\Element\Hidden;
use Core\Forms\Element\Kvgroup;
use Core\Forms\Element\Numeric;
use Core\Forms\Element\Password;
use Core\Forms\Element\Radio;
use Core\Forms\Element\Select;
use Core\Forms\Element\Selects;
use Core\Forms\Element\Tags;
use Core\Forms\Element\Text;
use Core\Forms\Element\TextArea;
use Core\Forms\Element\Url;
use Core\Forms\Element\ValidateCode;

class Element
{

    public static function text(Form &$t, &$element)
    {
        $field = new Text($element['name'], $element['attributes']);
        return $field;
    }

    public static function url(Form &$t, &$element)
    {
        $field = new Url($element['name'], $element['attributes']);
        $element['validate'][] = array(
            'v' => 'Url',
            'message' => '请输入合法的Url地址',
        );
        return $field;
    }

    public static function numeric(Form &$t, &$element)
    {
        $field = new Numeric($element['name'], $element['attributes']);
        $field->addFilter('int');
        $field->addFilter('intval');
        return $field;
    }

    public static function check(&$t, &$element)
    {
        $field = new Check($element['name'], $element['attributes']);
        if (!isset($element['value'])) {
            $element['value'] = 1;
        }
        $field->setDefault($element['value']);
        return $field;
    }

    public static function hidden(Form &$t, &$element)
    {
        $field = new Hidden($element['name'], $element['attributes']);
        return $field;
    }

    public static function tags(Form &$t, &$element)
    {
        $field = new Tags($element['name'], $element['attributes']);
        return $field;
    }

    public static function Autoinput(Form &$t, &$element)
    {
        $field = new Autoinput($element['name'], $element['attributes']);
        return $field;
    }

    public static function Chosen(Form &$t, &$element)
    {
        if (!isset($element['options'])) {
            $element['options'] = array();
        }
        $field = new Chosen($element['name'], $element['options'], $element['attributes']);
        return $field;
    }

    public static function group(Form &$t, &$element)
    {
        $field = new Group($element['name'], $element['attributes']);
        $field->setGroup($element['group']);
        $t->addField($element['group']);
        return $field;
    }

    public static function groupTabs(Form &$t, &$element)
    {
        $field = new GroupTabs($element['name'], $element['attributes']);
        $field->setGroupTabs($element['groupTabs']);
        $t->addField($element['groupTabs']);
        return $field;
    }

    public static function validateCode(Form &$t, &$element)
    {
        $field = new ValidateCode($element['name'], $element['attributes']);
        return $field;
    }

    public static function kvgroup(Form &$t, &$element)
    {
        $field = new Kvgroup($element['name'], $element['attributes']);
        return $field;
    }

    public static function radio(Form &$t, &$element)
    {
        $field = new Radio($element['name'], $element['attributes']);
        return $field;
    }

    public static function select(Form &$t, &$element)
    {
        if (is_string($element['options'])) {
            $element['options'] = call_user_func($element['options'], $element);
        }
        $field = new Select($element['name'], $element['options'], $element['attributes']);
        if (!empty($element['options'])) {
            $element['validate'][] = array(
                'v' => 'InclusionIn',
                'message' => '您的输入内容超出了范围',
                'domain' => array_keys($element['options']),
            );
        }
        return $field;
    }

    public static function selects(Form &$t, &$element)
    {
        if (is_string($element['options'])) {
            $element['options'] = call_user_func($element['options'], $element);
        }
        $field = new Selects($element['name'], $element['options'], $element['attributes']);
        return $field;
    }

    public static function date(Form &$t, &$element)
    {
        $field = new Date($element['name'], $element['attributes']);
        return $field;
    }

    public static function password(Form &$t, &$element)
    {
        $field = new Password($element['name'], $element['attributes']);
        return $field;
    }

    public static function email(Form &$t, &$element)
    {
        $field = new Email($element['name'], $element['attributes']);
        $element['validate'][] = array(
            'v' => 'Email',
            'message' => '请输入合法的email地址',
        );
        return $field;
    }

    public static function textarea(Form &$t, &$element)
    {
        $field = new TextArea($element['name'], $element['attributes']);
        return $field;
    }

    public static function validationAttributes($element)
    {
        $attributes = [];
        switch ($element['widget']) {
            case 'color':
                $attributes['data-fv-hexcolor'] = 'true';
                $attributes['data-fv-hexcolor-message'] = '必须是合法的颜色';
                break;
            case 'email':
                $attributes['data-fv-emailaddress'] = 'true';
                $attributes['data-fv-emailaddress-message'] = '必须是合法可用的邮件地址';
                break;
            case 'url':
                $attributes['data-fv-uri'] = 'true';
                $attributes['data-fv-uri-message'] = '必须是合法可用的URL地址';
                break;
        }
        if (isset($element['required']) && $element['required'] === true) {
            $attributes['data-fv-notempty'] = 'true';
            $attributes['data-fv-notempty-message'] = '该值不能为空';
        }
    }
}
